<template lang="pug">
el-dialog(:title="title" :visible.sync="gli" :before-close="close" width="85%" append-to-body)
    el-form(:v-model="f" label-position="right" style="width:100%")
        el-form-item
            el-button(type="primary" icon="el-icon-document" @click="save" v-if="ef") 保存
            el-button(type="primary" icon="el-icon-edit-outline" @click="pl=!pl" v-if="false") 批量设置 
            el-row.mgts
                // el-input.elinput1(type="text" placeholder="线上销售规格" style="width:100px" v-if="pl" v-model="cstockNum")
                el-input.elinput1(type="number" placeholder="售价（元）" style="width:100px" v-if="pl" v-model="cprice")
                el-input.elinput1(type="number" placeholder="拼团人数" style="width:100px" v-if="pl" v-model="cptType")
                el-input.elinput1(type="number" placeholder="拼团价" style="width:100px" v-if="pl" v-model="cptPrice")
                el-input.elinput1(type="text" placeholder="商品别名" style="width:100px" v-if="pl" v-model="cpgoodsName")
                // el-select.elinput1(v-model="cdeliveryType" placeholder="配送方式" v-if="pl" @change="tchange" style="width:100px" v-if="er") 
                //     el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
                // el-select.elinput1(v-model="cdeliveryType" placeholder="拼团配送方式" v-if="pl" @change="tchange" style="width:100px" v-if="er") 
                //     el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
                el-button(type="primary" v-if="pl" @click="changeall" style="margin-left:10px") 确定
                el-button(type="primary" v-if="pl" @click="pl=false") 取消
    el-table(:data="td" style="width: 100%" height="450" v-loading="loading" element-loading-text="数据加载中..." border v-if="ef")
        el-table-column(label="店铺名称")
            template(slot-scope="scope")
                span {{scope.row.shopName}} 
                // el-button(icon="el-icon-plus" circle  type="primary" style="float:right" @click="changerow(scope.$index,scope.row.shopName,scope.row.shopId,scope.row.spec)" v-if="scope.row.nn")
        el-table-column(prop="count" label="线上销售规格")
            // template(slot-scope="scope")
            //     div
            //         el-input(type="number" v-model="scope.row.count" @blur="checkspec(scope.$index,scope.row)")
        el-table-column(prop ="spec" label="规格单位")
        el-table-column(prop="price" label="售价（元）")
            template(slot-scope="scope")
                el-input(type="number" v-model="scope.row.price")
        el-table-column(prop="ptType" label="拼团人数")
            template(slot-scope="scope")
                el-input(type="number" v-model="scope.row.ptType")
        el-table-column(prop="ptPrice" label="拼团价")
            template(slot-scope="scope")
                el-input(type="number" v-model="scope.row.ptPrice")
        el-table-column(prop="goodsName" label="商品别名")
            template(slot-scope="scope")
                el-input(type="text" v-model="scope.row.goodsName")
        // el-table-column(prop="tableobj.yunfei" clabel="运费" v-if="er")
        // el-table-column(label="配送方式" v-if="er")
        //     template(slot-scope="scope")
        //         el-select(v-model="tableobj.totype" placeholder="请选择配送方式" :disabled="scope.$index==0?false:true"
        //             @focus="getscopeindex(scope.$index)" @change="changedata") 
        //             el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
        // el-table-column(label="拼团配送方式" v-if="er")
        //     template(slot-scope="scope")
        //         el-select(v-model="tableobj.pttotype" placeholder="请选择配送方式" :disabled="scope.$index==0?false:true"
        //             @focus="getscopeindexs(scope.$index)" @change="changedatas") 
        //             el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
        // el-table-column(label="操作")
        //     template(slot-scope="scope")
        //         el-button(@click="func(scope.$index)" type="danger" icon="el-icon-delete" circle v-if="!scope.row.nn")
    el-table(:data="td" style="width: 100%" height="450" v-loading="loading" element-loading-text="数据加载中..." border v-if="er")
        el-table-column(prop="shopName" label="店铺名称")
        el-table-column(prop="count" label="线上销售规格")
        el-table-column(prop ="spec" label="规格单位")
        el-table-column(prop="price" label="售价（元）")
        // el-table-column(prop="stockNum" label="库存")
        el-table-column(prop="ptType" label="拼团人数")
        el-table-column(prop="ptPrice" label="拼团价")
        el-table-column(prop="goodsName" label="商品别名")
        // el-table-column(prop="deliveryPrice" label="运费" v-if="er")
        //     template(slot-scope="scope")
        //         el-input(type="number" v-model="tableobj.yunfei" :disabled="true")
        // el-table-column(label="配送方式" v-if="er")
        //     template(slot-scope="scope")
        //         el-select(v-model="tableobj.totype" placeholder="请选择配送方式"
        //             @focus="getscopeindex(scope.$index)" @change="changedata") 
        //             el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
        // el-table-column(label="拼团配送方式" v-if="er")
        //     template(slot-scope="scope")
        //         el-select(v-model="tableobj.pttotype" placeholder="请选择配送方式"
        //             @focus="getscopeindexs(scope.$index)" @change="changedatas") 
        //             el-option(v-for="i in opt" :key="i.vlaue" :label="i.label" :value="i.value")
</template>
<script>
    import lockr from 'lockr'
    import {
        TSTypeOperator,
        templateElement
    } from 'babel-types';

    export default {
    
        data() {
    
            return {
    
                rowspecarr: [],
                rowspecarrs: [],
                title: '',
                radio: '0',
                visible2: false,
                ef: !this.$store.state.shiro,
                er: this.$store.state.shiro,
                cstockNum: '',
                cprice: '',
                cptPrice: '',
                cunitid: 1,
                gli: false,
                loading: false,
                cdeliveryType: 0,
                f: {},
                pl: false,
                td: [],
                // {shopName:"1"},{shopName:"2"},{shopName:"3"},{shopName:"4"},{shopName:"5"}
                opt: [{
                    value: 0,
                    label: "全部"
                }, {
                    value: 2,
                    label: "自提"
                }],
                gtype: '0',
                classshoplist: [],
                vid: -1,
                scopelineindex: -1,
                scopelineindexs: -1,
                opts: [],
                rowsshopname: "",
                thisfruitspec: "",
                cpgoodsName: "",
                tableobj: {
                    yunfei: 8,
                    totype: 0,
                    pttotype: 2
                },
                goodconts: []
            }
        },
        computed: {
    
            getstate() {
    
                this.rowspecarrs = []
                return this.$store.state.glistinfoclass;
    
            },
    
            getchange() {
    
                return this.$store.state.glistinfo;
    
            }
    
        },
    
        watch: {
    
            getstate(val) {
    
                this.gli = val
    
            }, //监视弹窗
    
            getchange(val) {
    
                this.vid = -1
                if (val.goodsId == undefined) {
                    return
                }
                this.classshoplist = new Array()
    
                var param = new URLSearchParams();
    
                param.append("xcxGoodsId", val.goodsId);
    
                this.title = '配置 ' + val.name + ' 的仓库信息'
    
                this.vid = val.goodsId
    
                this.thisfruitspec = val.spec

                if(JSON.parse(val.specCount).length<=0){
                    this.$message.error('请编辑商品线上销售规格')
                }
    
                this.goodconts = [].concat(JSON.parse(val.specCount))
    
                // console.log(this.goodconts)
    
                this.listGoodsUnit()
    
                this.getData(param)
    
            } //监视传参
    
        },
    
        methods: {
    
            func(i) {
    
                this.td.splice(i, 1)
    
            },
    
            changerow(i, ...v) {//新增一行
    
                var obj = {}
    
                obj["shopName"] = v[0]
    
                obj["shopId"] = v[1]
    
                obj["deliveryPrice"] = 12
    
                obj["spec"] = v[2]
    
                this.td.splice(i + 1, 0, obj)
    
                this.changetablebtn()
    
            },
    
            getsameshopname() {//获取相同店铺名称
    
                var arr = []
    
                var arrs = []
    
                arr.push(0)
    
                for (var i = 1; i < this.td.length; i++) {
    
                    if (this.td[i].shopName == this.td[i - 1].shopName) {
    
                        arr.push(i)
    
                    } else {
    
                        arrs.push(arr)
    
                    }
    
                }
    
                return arrs
    
            },
    
            rowspecchange(v) { //规格的值
    
                //线上销售规格存在一个对象值内
    
                //然后td是被赋值的这个对象,会入参scope.row里的这个对象到组件
    
                //组件改变的时候,push这个值到父组件的数组里
    
                //save的时候,遍历这个数组,获取对应的对象给到相关字段提交
    
                // console.log(v)
    
                if (this.rowspecarrs.length == 0) {
    
                    this.rowspecarrs.push(v)
    
                } else {
    
                    var zf
    
                    for (var i = 0; i < this.rowspecarrs.length; i++) {
    
                        var tr = this.rowspecarrs[i]
    
                        if (tr.index == v.index) {}
    
                    }
    
                }
    
            },
    
            listGoodsUnit() {//老版获取单位
    
                var _this = this
    
                axios.get('/listGoodsUnit.do').then(r => {
    
                    if (r.data.code == 200) {
    
                        _this.opts = [].concat(r.data.response.goodsUnitList)
    
                    } else {
    
                        _this.$message.error(r.data.message)
    
                    }
    
                })
    
            },
    
            getscopeindex(i) {
    
                //这里是获取被点击的行
    
                this.scopelineindex = i
    
            },
    
            getscopeindexs(i) {
    
                //这里是获取被点击的行
    
                this.scopelineindexs = i
    
            },
    
            changedata(v) {
    
                //这里是获取并改变的值el-select
    
                var arr = []
    
                arr = [].concat(this.td)
    
                this.td = []
    
                arr[this.scopelineindex].deliveryType = v
    
                this.td = [].concat(arr)
    
            },
    
            changedatas(v) {
    
                //这里是获取并改变的值el-select
    
                var arr = []
    
                arr = [].concat(this.td)
    
                this.td = []
    
                arr[this.scopelineindexs].unit = v
    
                this.td = [].concat(arr)
    
            },
    
            checkspec(i, r) {
    
                if (r.count) {
    
                    for (var tt = 0; tt < this.td.length; tt++) {
    
                        if (r.shopId == this.td[tt].shopId) {
    
                            if (tt != i) {
    
                                if (this.td[tt].count == r.count) {
    
                                    this.$message.error(this.td[tt].shopName + "的线上销售规格不能相同")
    
                                }
    
                            }
    
                        }
    
                    }
    
                }
    
            },
    
            close() {
                //弹窗关闭清空表格的代码
                this.td = []
    
                this.title = ''
    
                this.classshoplist = []
    
                this.vid = -1
    
                this.scopelineindex = -1
    
                this.scopelineindexs = -1
    
                this.$store.state.glistinfoclass = false
    
                this.$store.state.glistinfo = {}
    
            },
    
            changeall() { //批量设置
    
                var _this = this
    
                var arrtd = [].concat(this.td)
    
                this.td = []
    
                for (var i = 0; i < arrtd.length; i++) {
    
                    var ts = arrtd[i]
    
                    if (_this.cstockNum != '') {
    
                        ts.count = _this.cstockNum
    
                    }
    
                    if (_this.cprice != '') {
    
                        ts.price = _this.cprice
    
                    }
    
                    if (_this.cptPrice != '') {
    
                        ts.ptPrice = _this.cptPrice
    
                    }
    
                    if (_this.cptType != '') {
    
                        ts.ptType = _this.cptType
    
                    }
    
                    // ts.deliveryType=_this.cdeliveryType
    
                    if (_this.cpgoodsName != '') {
    
                        ts.goodsName = _this.cpgoodsName
    
                    }
    
                }
    
                _this.td = [].concat(arrtd)
    
                _this.changetablebtn()
    
                _this.pl = !_this.pl
    
            },
    
            tchange(v) {
    
                this.cdeliveryType = v
    
            },
    
            tchanges(v) {
    
                this.cunitid = v
    
            },
    
            changetablebtn() {
    
                if (this.td.length == 0) {
    
                    this.$message.error('店铺错误,请联系管理员')
    
                    return
    
                }
    
                this.td[0].nn = true
    
                if (this.$store.state.shiro) {
    
                    for (var i = 1; i < this.td.length; i++) {
    
                        if (this.td[i].shopId == this.td[i - 1].shopId) {
    
                            this.td[i].nn = false
    
                        } else {
    
                            this.td[i].nn = true
    
                        }
    
                    }
    
                } else {
    
                    for (var i = 1; i < this.td.length; i++) {
    
                        this.td[i].nn = false
    
                    }
    
                }
    
            },
    
            checklistr() {//遍历对每个填写的单元格做排查
    
                for (var i = 0; i < this.td.length; i++) {
    
                    if (this.td[i].count === '' || this.td[i].count === undefined) {
    
                        _this.$message.error('线上销售规格不能为空')
    
                        return false
    
                    }
                    if (this.td[i].count <0) {
    
                        _this.$message.error('线上销售规格不能小于0')
    
                        return false
    
                    }
    
                    if (this.td[i].spec === '' || this.td[i].spec === undefined) {
    
                        _this.$message.error('库存单位不能为空')
    
                        return false
    
                    }
                    if (this.td[i].spec <0) {
    
                        _this.$message.error('库存单位不能小于0')
    
                        return false
    
                    }
    
                    if (this.td[i].price === '' || this.td[i].price === undefined) {
    
                        _this.$message.error('售价不能为空')
    
                        return false
    
                    }

                    if (this.td[i].price <0) {
    
                        _this.$message.error('售价不能小于0')
    
                        return false
    
                    }
    
                    if (this.td[i].ptPrice === '' || this.td[i].ptPrice === undefined) {
    
                        _this.$message.error('拼团价不能为空')
    
                        return false
    
                    }

                    if (this.td[i].ptPrice <0) {
    
                        _this.$message.error('拼团价不能小于0')
    
                        return false
    
                    }
    
                }
    
                return true
    
            },
    
            save() {//保存
                var _this = this
                var xcxShopPrices = []
                for (var i = 0; i < this.td.length; i++) {
    
                    if (this.td[i].price === '' || this.td[i].price === undefined) {
    
                        _this.$message.error('售价不能为空')
    
                        return
    
                    }
                    if (parseFloat(this.td[i].price)<0) {
    
                        _this.$message.error('售价不能小于0')
    
                        return
    
                    }
    
                    
    
                    if (this.td[i].ptPrice === '' || this.td[i].ptPrice === undefined) {
    
                        _this.$message.error('拼团价不能为空')
    
                        return
    
                    }
                    if (parseFloat(this.td[i].ptPrice)<0) {
    
                        _this.$message.error('拼团价不能小于0')
    
                        return
    
                    }

                    if (parseFloat(this.td[i].ptType)<0) {
    
                        _this.$message.error('拼团人数不能小于0')
    
                        return
    
                    }
    
                    var ptType = 0
    
                    if (!this.td[i].ptType) {
    
                        ptType = 0
    
                    } else {
    
                        ptType = this.td[i].ptType
    
                    }
                    //此处以json方式提交
                    var str = {
                        "goodsId": this.vid,
                        "shopId": this.td[i].shopId,
                        "count": this.td[i].count,
                        "spec": this.thisfruitspec,
                        "price": this.td[i].price,
                        "ptType": ptType,
                        "goodsName": this.td[i].goodsName,
                        "ptPrice": this.td[i].ptPrice,
                        "oldPrice":this.td[i].oldPrice    
                    }
                    if(this.td[i].shopPriceId){
                        str["shopPriceId"]=this.td[i].shopPriceId
                    }
                    xcxShopPrices.push(str)
                }
    
                axios.post('batchSaveXcxShopPrice.do', xcxShopPrices, {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).then(r => {
    
                    if (r.data.code == 200) {
    
                        _this.$message.success("保存成功")
                        _this.close()
    
                    } else {
    
                        _this.$message.error(r.data.messages)
    
                    }
    
                }).catch(e => {
    
                    _this.$message.error('保存失败,请确认是否有缺少填写')
    
                })
    
            },
    
            getshoplist(f) {
                var _this = this
                axios.get('/shop/listShop.do').then(r => {
                    if (r.data.code != 200) {
                        return
                    }
                    _this.classshoplist = [].concat(r.data.response.shopList)
                    f()
                })
            },
            findshopdef(arr, id) {
                for (var tt = 0; tt < arr.length; tt++) {
    
                    if (arr[tt].shopId == id) {
    
                        console.log("遍历查找2", arr[tt].shopId, id, tt)
    
                        return true
    
                    }
    
                }
    
                return false
    
            },
    
            getData(v) {
    
                this.loading = true
                var _this = this
                this.td = []
                this.getshoplist(function() {
                    axios.get('/listXcxShopPrice.do', {params: v}).then(r => {
                        if(r.data.message=="not login"&&r.data.code==500){
                            this.$message.error("访问者,请登录")
                            router.push('/Login');
                            return
                        }
                        // console.log("listXcxshopprice1",r)
                        if (r.data.code == 200) {
    
                            if (_this.$store.state.shiro) { //超级管理员
    
                                if (r.data.response.shopPrices.length == 0) {
    
                                    // console.log("超管,库存为空")
                                    var Configurationofinventory = []
    
                                    for (var i = 0; i < _this.classshoplist.length; i++) {
    
                                        _this.classshoplist[i].deliveryPrice = 12
                                        _this.classshoplist[i].spec = _this.thisfruitspec
                                        if (_this.goodconts.length <= 0) {
                                            _this.$message.error('线上销售规格为空,请返回商品发布填写线上销售规格或联系管理员')
                                            return
                                        }
    
                                        for (var mm = 0; mm < _this.goodconts.length; mm++) {
    
                                            var newobj = {}
                                            Object.assign(newobj, _this.goodconts[mm], _this.classshoplist[i])//合并门店和线上销售规格
                                            Configurationofinventory.push(newobj)
                                            // console.log("thisgetData",Configurationofinventory)
    
                                        }
    
                                    }
    
                                    // console.log("库存为空时的数据混合",Configurationofinventory)
                                    _this.td = [].concat(Configurationofinventory)
                                    _this.loading = false
    
                                } else {
    
                                    // console.log("超管,库存不为空")
                                    var dr = r.data.response.shopPrices
                                    _this.tableobj.yunfei=dr[0].deliveryPrice
                                    _this.tableobj.totype=dr[0].deliveryType
                                    _this.tableobj.pttotype=dr[0].ptDeliveryType
    
                                    for (var n = 0; n < dr.length; n++) {//
    
                                        for (var i = 0; i < _this.classshoplist.length; i++) {
    
                                            // _this.classshoplist[i].deliveryPrice=12
    
                                            if (dr[n].shopId == _this.classshoplist[i].shopId) {
                                                var ts = _this.classshoplist[i]
                                                dr[n].shopName = ts.shopName
                                                dr[n].spec = _this.thisfruitspec
                                                if (dr[n].deliveryPrice == null) {
                                                    dr[n].deliveryPrice = 0
                                                }
                                            }
    
                                        }
    
                                    }
    
                                    for (var f = 0; f < _this.classshoplist.length; f++) { //遍历所有店铺
    
                                        // console.log("遍历查找1")
                                        var Qun = true //标识    
    
                                        for (var tt = 0; tt < dr.length; tt++) {
    
                                            // console.log("遍历查找2",dr[tt].shopId,tt)
                                            if (dr[tt].shopId == _this.classshoplist[f].shopId) {
                                                Qun = false //此处代表dr里找到了店铺[f],那么设置为false,并跳出
                                                break;
                                            }
    
                                        }
    
                                        if (Qun) { //如果此处为true,就代表没找到,那么执行添加
    
                                            // console.log("遍历查找3")
                                            // obj["deliveryPrice"]=12
                                            for(var ii = 0;ii<_this.goodconts.length;ii++){

                                                var obj = {}
                                                obj["spec"] = _this.thisfruitspec
                                                obj["shopId"] = _this.classshoplist[f].shopId
                                                obj["shopName"] = _this.classshoplist[f].shopName
                                                obj["count"] = _this.goodconts[ii].count
                                                obj["price"] = _this.goodconts[ii].price
                                                dr.push(obj)

                                            }
                                        }
                                    }
    
                                    _this.td = [].concat(dr)
                                    _this.changetablebtn()
                                    _this.loading = false
    
                                }
    
                            } else if (!_this.$store.state.shiro) { //店长
    
                                if (r.data.response.shopPrices.length == 0) {
                                    var arrofshoplist = []
                                    // console.log("店长,库存为空")
                                    for (var i = 0; i < _this.classshoplist.length; i++) {
    
                                        // _this.classshoplist[i].deliveryPrice = 12
                                        if (_this.classshoplist[i].shopId == _this.$store.state.TOPshopID) {
                                            var ts = _this.classshoplist[i]
                                            ts.spec = _this.thisfruitspec
                                            if(_this.goodconts.length==0){//为空直接判断线上销售规格，返回说明
                                                _this.$message.error('线上销售规格为空,请返回商品发布填写线上销售规格或联系管理员')
                                                return
                                            }

                                            for(var nn=0;nn<_this.goodconts.length;nn++){//合并线上销售规格和门店

                                                var newList={}
                                                Object.assign(newList,_this.goodconts[nn],ts)
                                                arrofshoplist.push(newList)

                                            }
                                        }
                                    }
                                    //
    
                                    _this.td = [].concat(arrofshoplist)
                                    _this.changetablebtn()
                                    // console.log('库存为空',_this.td)
                                    _this.loading = false
    


                                } else {
    
                                    var arrofshoplist = new Array()
                                    // console.log("店长,库存不为空")
                                    var dr = r.data.response.shopPrices
                                    _this.tableobj.yunfei=dr[0].deliveryPrice
                                    _this.tableobj.totype=dr[0].deliveryType
                                    _this.tableobj.pttotype=dr[0].ptDeliveryType
    
                                    for (var i = 0; i < dr.length; i++) {
    
                                        if (dr[i].shopId == _this.$store.state.TOPshopID) {
                                            dr[i].shopName = _this.$store.state.TOPshopName
                                            dr[i].spec = _this.thisfruitspec
                                            arrofshoplist.push(dr[i])
                                        }
    
                                    }
                                    if(arrofshoplist.length==0){
                                        for(var ii = 0;ii<_this.goodconts.length;ii++){

                                            var obj = {}
                                            obj["spec"] = _this.thisfruitspec
                                            obj["shopId"] = _this.$store.state.TOPshopID
                                            obj["shopName"] = _this.$store.state.TOPshopName
                                            obj["count"] = _this.goodconts[ii].count
                                            obj["price"] = _this.goodconts[ii].price
                                            arrofshoplist.push(obj)

                                        }
                                    }
    
                                    _this.td = [].concat(arrofshoplist)
                                    _this.changetablebtn()
                                    _this.loading = false
    
                                }
    
                            }
    
                            // console.log("库存配置请求方案",_this.td)
    
                        } else {
    
                            _this.$message(r.data.message)
                            _this.loading = false
    
                        }
    
                    })
    
                })
    
    
    
            }
    
        }
    
    }
</script>
<style scoped>
    .elinput1 {
    
        margin-left: 5px;
    
    }
    
    
    
    .mgts {
    
        margin-top: 5px;
    
        margin-bottom: 5px;
    
    }
</style>
